<template>
  <vxe-modal :loading="loadings" :title="titles" v-model="addxmls" width="95%" showFooter show-zoom resize
             height="96vh">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="68px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="科目" prop="km">
            <el-input
              v-model="queryParams.km"
              placeholder="请输入科目"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="锁定状态" prop="sdZt">
            <el-select v-model="queryParams.sdZt" placeholder="请选择锁定状态" clearable>
              <el-option
                v-for="dict in dict.type.sdzt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="凭证日期">
            <el-date-picker
              v-model="daterangePzTime"
              style="width: 100%"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row :row-class-name="tableRowClassName"
              :stripe="false" height="300"
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              class="fixedTableOne" v-loading="loading" :data="xmlsList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column label="科目序号" align="center" prop="kmXh"/>
      <el-table-column show-overflow-tooltip label="科目" align="center" prop="km" width="300"/>
      <el-table-column label="凭证日期" align="center" prop="pzTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pzTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="凭证号" align="center" prop="pzh"/>
      <el-table-column label="凭证种类" align="center" prop="pzZl">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.pzZl }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="借/贷" align="center" prop="jd">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lstb_xmls_jd" :value="scope.row.jd"/>
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="je"/>
      <el-table-column label="摘要" align="center" prop="zy"/>
      <el-table-column label="锁定状态" align="center" prop="sdZt" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sdzt" :value="scope.row.sdZt"/>
        </template>
      </el-table-column>
      <el-table-column label="锁定(解锁)日期" align="center" prop="sTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="锁定(解锁)人" align="center" prop="sName"/>
      <el-table-column label="申请人" align="center" prop="createBy"/>
      <el-table-column label="申请时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请单位" align="center" prop="deptName"/>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--          >修改-->
      <!--          </el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--          >删除-->
      <!--          </el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 预览组件 -->
    <KkFileView ref="preview"/>

    <!-- 项目流水详情 -->
    <XmLsXq ref="xmLsXq"/>

  </vxe-modal>
</template>

<script>
import {
  addXmls,
  deleteLstbXmlsFjByFjIds,
  delXmls,
  getXmls,
  getXmLsId,
  insertXmlsFjByXmlsId,
  listXmls,
  selectXmlsFjList,
  updateXmls
} from "@/api/fztz/xmls";
import {listFykm} from "@/api/fztz/fykm";
import {getToken} from "@/utils/auth";
import KkFileView from "@/views/components/KKFileView/kkFileView.vue";
import XmLsXq from "@/views/fztz/xmls/components/XmLsXq.vue";

export default {
  name: "LsCk", //流水查看
  components: {XmLsXq, KkFileView},
  dicts: ['lstb_xmls_pzzl', 'lstb_xmls_jd', 'sdzt'],
  data() {
    return {
      km: null, //点击修改时查询出的科目
      loadings: false,
      getXmLsId: null, //从后端获取到的项目流水ID
      rowData: [], //从流水填报页面传过来的值
      addxmls: false, //控制整个弹窗开关
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedLstbXmlsFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目流水表格数据
      xmlsList: [],
      // 项目流水附件表格数据
      lstbXmlsFjList: [],
      // 弹出层标题
      title: "",
      titles: "",
      // 是否显示弹出层
      open: false,
      // 文件大小时间范围
      daterangePzTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lstbId: null,
        km: null,
        pzTime: null,
        idWhere: null,
        userId: null,
        deptId: null,
        sdZt: null,
        xmsbId: null,
      },
      // 表单参数
      form: {},
      kmOptions: null, //查询到的所有科目
    };
  },
  created() {
  },
  methods: {
    //打开项目流水详情组件
    handleButtonClick(row) {
      this.$refs.xmLsXq.handleOpen(row)
    },
    //行内锁定状态为锁定时, 将该行背景颜色改为灰色
    tableRowClassName: function ({row, rowIndex}) {
      if (row.sdZt === '锁定') {
        return 'warm-row';
      }
    },
    //打开整个弹窗
    handleOpen(row) {
      this.loadings = true
      this.rowData = row
      this.titles = '【' + row.xmName + '】' + '流水填报'
      this.addxmls = true
      this.getList()
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 查询项目流水列表 */
    getList() {
      this.loading = true;
      this.queryParams.lstbId = this.rowData.lstbId
      this.queryParams.params = {};
      if (null != this.daterangePzTime && '' != this.daterangePzTime) {
        this.queryParams.params["beginPzTime"] = this.daterangePzTime[0];
        this.queryParams.params["endPzTime"] = this.daterangePzTime[1];
      }
      listXmls(this.queryParams).then(response => {
        this.xmlsList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.loadings = false
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        xmlsId: this.getXmLsId,
        lstbId: this.rowData.lstbId,
        xmName: this.rowData.xmName,
        xmLx: this.rowData.xmLx,
        km: null,
        pzTime: null,
        pzZl: '记账凭证',
        pzh: null,
        jd: '借',
        je: null,
        zy: null,
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null,
        kmXh: null,
        sdZt: null,
        sTime: null,
        sName: null,
        xmsbId: this.rowData.xmsbId,
      };
      this.lstbXmlsFjList = [];
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePzTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.xmlsId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fztz/xmls/export', {
        ...this.queryParams
      }, `xmls_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>

<style lang="scss">
.warm-row {
  background-color: #eaeaea !important;
}
</style>
